package com.lm.security.browser.service;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.User;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.stereotype.Component;

/**
 * @author lm_jcf
 * @date 2018/4/27 14:50
 */
@Component("userDetailsService")
public class MyUserDetailsService implements UserDetailsService {

    private Logger LOG = LoggerFactory.getLogger(MyUserDetailsService.class);

    @Autowired
    private PasswordEncoder passwordEncoder;

    @Override
    public UserDetails loadUserByUsername(String username) throws UsernameNotFoundException {
        LOG.info("登录用户名："+ username);
        //根据用户查找信息
        //根据查找到的用户信息判断用户是否冻结
//        isAccountNonExpired() 返回验证是否过期。
//        isCredentialsNonExpired() 告诉security 密码是否过期了
//        isAccountNonLocked() 账户是否锁定
//        isEnabled() 是否被冻结

        //存储数据的时候加密：passwordEncoder.encode(password);

        String password = passwordEncoder.encode("123456");
        LOG.info("数据库密码："+ password);

        return new User(username,password
                ,true,true,true,true,AuthorityUtils.commaSeparatedStringToAuthorityList("admin"));

    }
}
